package com.doform.android.xianggucheng.utils.http;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Map;

import okhttp3.MediaType;
import okhttp3.Response;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017/5/27.
 * GitHub：https://github.com/cxydxpx
 */

public class HttpUtils {

    /**
     * get同步请求
     *
     * @param url
     * @return
     */
    public static Response getRequest(String url) {

        try {
            return OkHttpUtils
                    .get()
                    .url(url)
                    .build().execute();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * get同步请求
     *
     * @param url
     * @return
     */
    public static void getRequest(String url, Callback callback) {
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(callback);

    }

    /**
     * Post请求
     *
     * @param url
     * @param callback
     */
    public static void postRequest(String url, String json, Callback callback) {
        OkHttpUtils
                .postString()
                .url(url)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(callback);

    }

    /**
     * Post请求
     *
     * @param url
     * @param stringCallback
     */
    public static void postRequest(String url, Map<String, String> mas, StringCallback stringCallback) {
        OkHttpUtils
                .post()
                .url(url)
                .params(mas)
                .build()
                .execute(stringCallback);

    }


    /**
     * Post请求
     *
     * @param url
     * @param callback
     */
    public static void postRequest(String url, String key, String value, Callback callback) {

        OkHttpUtils
                .post()
                .url(url)
                .addParams(key, value)
                .build()
                .execute(callback);

    }

}
